package com.atguigu.guli.service.oss.controller;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.oss.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author lsystart
 * @create2021-10-30 9:46
 */

@CrossOrigin
@RestController
@RequestMapping("/admin/oss")
public class FileController {

    @Autowired
    FileService fileService;

    @PostMapping("/upload")
    public R upload(MultipartFile file,String module) throws IOException {

        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String fileURL = fileService.upload(inputStream, originalFilename, module);
        return R.ok().data("fileURL",fileURL);
    }

    @DeleteMapping("/delete")
    public R delete(String fileURL,String module){
        fileService.delete(fileURL,module);
        return R.ok();
    }
}
